export default {
	/**
	 * 格式化日期
	 * @param {string | number | Date} value 指定日期
	 * @param {string} format 格式化的规则
	 * @example
	 * ```js
	 * formatDate();
	 * formatDate(1603264465956);
	 * formatDate(1603264465956, "h:m:s");
	 * formatDate(1603264465956, "Y年M月D日");
	 * ```
	 */
	formatDate(value = Date.now(), format = "Y-M-D h:m:s") {
		const formatNumber = n => `0${n}`.slice(-2);
		const date = new Date(value);
		const formatList = ["Y", "M", "D", "h", "m", "s"];
		const resultList = [];
		resultList.push(date.getFullYear().toString());
		resultList.push(formatNumber(date.getMonth() + 1));
		resultList.push(formatNumber(date.getDate()));
		resultList.push(formatNumber(date.getHours()));
		resultList.push(formatNumber(date.getMinutes()));
		resultList.push(formatNumber(date.getSeconds()));
		for (let i = 0; i < resultList.length; i++) {
			format = format.replace(formatList[i], resultList[i]);
		}
		return format;
	},
	
	formatTime(time) {
		const d = new Date(time).getTime()
		const now = Date.now()
		const diff = (now - d) / 1000
		if (diff < 30) {
			return '刚刚'
		} else if (diff < 3600) {
			return Math.ceil(diff / 60) + '分钟前'
		} else if (diff < 3600 * 24) {
			return Math.ceil(diff / 3600) + '小时前'
		} else if (diff < 3600 * 24 * 2) {
			return '1天前'
		}
	
	},
	getWelcomeText() {
		let h = new Date().getHours()
		if (h >= 6 && h < 9) {
			return "早上好";
		}
		if (h >= 9 && h < 12) {
			return "上午好";
		}
		if (h >= 12 && h < 14) {
			return "中午好";
		}
		if (h >= 14 && h < 18) {
			return "下午好";
		}
		if (h >= 18 && h < 23) {
			return "晚上好";
		}
		return "凌晨啦";
	},
	checkLogin(callback, isLogin = true, nologin) {
		let user = cache.get('UserInfo')
	
		if (!user && isLogin) {
			uni.$rui.route("/pages/user/login/login")
			return false;
		}
		if (typeof callback == "function" && user) {
			callback(user || {})
			return false;
		}
		if (typeof nologin === 'function') {
			nologin()
			return false;
		}
	},
	
	sortObjectByKey(array, key) {
		return array.sort(function(a, b) {
			var x = a[key];
			var y = b[key];
			return ((x < y) ? ((x > y) ? 1 : 0) : -1);
	
		});
	},
	
	getNowDay() {
		let n = new Date()
		let y = n.getFullYear()
		let m = n.getMonth() + 1
			m = m < 10 ? '0' + m : m;
		let d = n.getDate()
		return y + '' + m + '' + d
	},
}